package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.pojo.lesson.Lesson;
import com.fjh.springboot_01.service.LessonFavoriteService;
import com.fjh.springboot_01.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lesson/favorite")
public class LessonFavoriteController {
    @Autowired
    private LessonFavoriteService lessonFavoriteService;

    @PostMapping
    public Result favorite(@RequestBody Map<String, Integer> params) {
        Integer lessonId = params.get("lessonId");
        if (lessonId == null) {
            return Result.error("课程ID不能为空");
        }

        // 获取当前用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            lessonFavoriteService.favorite(lessonId, userId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("收藏失败：" + e.getMessage());
        }
    }

    @DeleteMapping
    public Result unfavorite(@RequestParam Integer lessonId) {
        if (lessonId == null) {
            return Result.error("课程ID不能为空");
        }

        // 获取当前用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            lessonFavoriteService.unfavorite(lessonId, userId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("取消收藏失败：" + e.getMessage());
        }
    }

    @GetMapping("/status")
    public Result getFavoriteStatus(@RequestParam Integer lessonId) {
        if (lessonId == null) {
            return Result.error("课程ID不能为空");
        }

        // 获取当前用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            boolean isFavorited = lessonFavoriteService.isFavorited(lessonId, userId);
            return Result.success(isFavorited);
        } catch (Exception e) {
            return Result.error("获取收藏状态失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result getUserFavorites() {
        // 获取当前用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            List<Lesson> favorites = lessonFavoriteService.getUserFavorites(userId);
            return Result.success(favorites);
        } catch (Exception e) {
            return Result.error("获取收藏列表失败：" + e.getMessage());
        }
    }

    @GetMapping("/count")
    public Result<Integer> getFavoriteCount(@RequestParam Integer lessonId) {
        if (lessonId == null) {
            return Result.error("课程ID不能为空");
        }

        try {
            Integer count = lessonFavoriteService.getFavoriteCount(lessonId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("获取收藏数量失败：" + e.getMessage());
        }
    }
} 